using lwzlz.Application.System;
using lwzlz.Application.System.Dto;
using lwzlz.Web.Areas.SysAdmin.Models;
using lwzlz.Web.Filter;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [MenuFilter]
    public class OrderController : AdminController
    {
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取首页列表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [MenuRoute("Index")]
        public JsonResult GetListPage()
        {
            LayTableModel result = new LayTableModel();
            OrderService service = new OrderService();
            try
            {
                int page = int.Parse(Request.Params["page"]);//当前页码
                int limit = int.Parse(Request.Params["limit"]);//每页数量
                string field = Request.Params["field"];//排序字段名
                string order = Request.Params["order"];//排序方式：desc, asc, null(空对象，默认排序)
                //参数
                Dictionary<string, string> dic = new Dictionary<string, string>();
                string[] par = { "MemberUserName", "MemberRealName", "OrderCode", "Status" };

                foreach (string item in par)
                {
                    if (!string.IsNullOrWhiteSpace(Request.Params[item]))
                    {
                        dic.Add(item, Request.Params[item]);
                    }
                }
                int count = 0;

                var list = service.GetListPage(page, limit, dic, field, order, out count);

                result.data = list;
                result.count = count;

            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 添加修改视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult AddOrEdit(int Id = 0)
        {
            OrderOutput model = null;
            OrderService service = new OrderService();
            if (Id > 0)
            {
                model = service.Get<OrderOutput>(Id);
            }
            else
            {
                model = new OrderOutput();
            }
            return View(model);
        }

        /// <summary>
        /// 查看视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult Look(int Id = 0)
        {
            OrderOutput model = null;
            OrderService service = new OrderService();
            ViewBag.MemberUserName = "";
            ViewBag.MemberRealName = "";
            ViewData["orderDetailList"] = null;
            ViewData["orderAddress"] = null;
            if (Id > 0)
            {
                model = service.Get<OrderOutput>(Id);
                if (model != null)
                {
                    //会员
                    var member = new MemberService().Get(model.MemberId);
                    if (member != null)
                    {
                        ViewBag.MemberUserName = member.UserName;
                        ViewBag.MemberRealName = member.RealName;
                    }

                    //商品
                    ViewData["orderDetailList"] = new OrderDetailService().GetList(model.Id);

                    //地址
                    ViewData["orderAddress"] = new OrderAddressService().GetModel(model.Id);
                }
            }
            else
            {
                model = new OrderOutput();
            }
            return View(model);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Add(OrderInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                int result = new OrderService().Add(model);
                if (result > 0)
                {
                    response.Success = true;
                    response.Message = "添加成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "添加失败";
                }

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Edit(OrderInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                int id = new OrderService().Edit(model);
                response.Success = true;
                response.Message = "修改成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [MenuRoute("Delete")]
        public JsonResult DeleteList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new OrderService().DeleteList(IdList);
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 记录视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult Record(int Id = 0)
        {
            ViewBag.Id = Id;
            return View();
        }

        /// <summary>
        /// 获取记录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [MenuRoute("Index")]
        public JsonResult GetRecord(int Id = 0)
        {
            LayTableModel result = new LayTableModel();
            try
            {
                var model = new List<OrderRecordOutput>();
                OrderRecordService service = new OrderRecordService();
                if (Id > 0)
                {
                    model = service.GetList(Id);
                }
                result.data = model;
            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 支付
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [MenuRoute("PaySuccess")]
        public JsonResult PaySuccessList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new OrderService().PaySuccessList(IdList);
                response.Success = true;
                response.Message = "支付成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [MenuRoute("ConfirmPay")]
        public JsonResult ConfirmPayList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new OrderService().ConfirmPayList(IdList);
                response.Success = true;
                response.Message = "确认成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [MenuRoute("Cancel")]
        public JsonResult CancelList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new OrderService().CancelList(IdList);
                response.Success = true;
                response.Message = "取消成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 发货物流
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        public ActionResult DeliverGoods(string IdList)
        {
            ViewData["IdList"] = IdList;
            return View();
        }

        /// <summary>
        /// 批量发货
        /// </summary>
        /// <param name="IdList"></param>
        /// <param name="LogisticsCompany"></param>
        /// <param name="LogisticsNumber"></param>
        /// <returns></returns>
        [MenuRoute("DeliverGoods")]
        public JsonResult DeliverGoodsList(string IdList, string LogisticsCompany, string LogisticsNumber)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new OrderService().DeliverGoodsList(IdList, LogisticsCompany, LogisticsNumber);
                response.Success = true;
                response.Message = "核销成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }
    }
}
